package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.ResponseResult;
import com.example.model.entity.UserInfo;
import com.example.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ling
 * @since 2024-08-15
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 添加
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody UserInfo userInfo){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",userInfo.getAccount());
        UserInfo resultUser = userInfoService.getOne(queryWrapper);
        if(!Objects.isNull(resultUser)){
            return ResponseResult.createErrorResult(ResponseResult.State.USER_EXIST);
        }
        userInfo.setCreateTime(new Date());
        userInfoService.save(userInfo);
        return new ResponseResult();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(Integer id){
        userInfoService.removeById(id);
        return new ResponseResult();
    }

    /**
     * 更新
     * @param userInfo
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody UserInfo userInfo){
        userInfoService.updateById(userInfo);
        return new ResponseResult();
    }


    @PostMapping("/forget-password")
    public ResponseResult forgetPassword(@RequestBody UserInfo userInfo){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",userInfo.getAccount());
        UserInfo userInfo1 = userInfoService.getOne(queryWrapper);
        if(Objects.isNull(userInfo1)){
            return ResponseResult.createErrorResult(410,"学号不存在");
        }
        if(!StringUtils.equals(userInfo1.getEmail(),userInfo.getEmail())){
            return ResponseResult.createErrorResult(410,"邮箱不正确");
        }
        userInfo1.setPassword(userInfo.getPassword());
        userInfoService.updateById(userInfo1);
        return new ResponseResult();
    }

    /**
     * 获取列表
     * @param page
     * @param size
     * @param account
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(Integer page,Integer size,String account,Integer roleId){
        if(!Objects.isNull(page)){
            PageHelper.startPage(page,size);
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(account)){
            queryWrapper.like("account","%"+account+"%");
        }
        if(!Objects.isNull(roleId)){
            queryWrapper.eq("role_id",roleId);
        }
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoService.list(queryWrapper));
        List<UserInfo> list = pageInfo.getList();
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list",list);
        resultMap.put("total",pageInfo.getTotal());
        return new ResponseResult(resultMap);
    }

    /**
     * 登录
     * @param userInfo
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserInfo userInfo){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",userInfo.getAccount());
        queryWrapper.eq("password",userInfo.getPassword());
        UserInfo result = userInfoService.getOne(queryWrapper);
        if(Objects.isNull(result)){
            return ResponseResult.createErrorResult(ResponseResult.State.PASSWORD_ACCOUNT_ERROR);
        }
        return new ResponseResult(result);
    }

}
